package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO<Product> findProductList(Integer pageNum, Integer pageSize, ProductVO productVO) {
        //已经拥有的
        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(Product.class);

        String name = productVO.getName();//物资名称
        Integer status = productVO.getStatus();             //物资状态

        Example.Criteria criteria = o.createCriteria();
        if (null != name && !"".equals(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (null != status && !"".equals(status)) {
            criteria.andLike("status", "%" + status + "%");
        }

        Long[] categoryKeys = productVO.getCategoryKeys();//物资所属分类
        System.out.println(Arrays.toString(categoryKeys));
        if (null != categoryKeys) {
            if (categoryKeys.length == 1) {
                criteria.andEqualTo("oneCategoryId", categoryKeys[0]);
            }
            if (categoryKeys.length == 2) {
                criteria.andEqualTo("oneCategoryId", categoryKeys[0]);
                criteria.andEqualTo("twoCategoryId", categoryKeys[1]);
            }
            if (categoryKeys.length == 3) {
                criteria.andEqualTo("oneCategoryId", categoryKeys[0]);
                criteria.andEqualTo("twoCategoryId", categoryKeys[1]);
                criteria.andEqualTo("threeCategoryId", categoryKeys[2]);
            }
        }
        List<Product> productList = productMapper.selectByExample(o);
        PageInfo<Product> info = new PageInfo<>(productList);

        return new PageVO<>(info.getTotal(), productList);
    }

    //添加物资
    @Override
    public void add(Product product) {
        //设置三级分类ID
        Long[] categoryKeys = product.getCategoryKeys();
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        //设置默认状态
        product.setStatus(0);
        //设置物资编号
        product.setPNum(UUID.randomUUID().toString());
        productMapper.insert(product);
    }

    //修改物资-回显
    @Override
    public Product edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    //修改物资
    @Override
    public void update(ProductVO productVO) {
        //TODO 图片修改后回显不了，数据库imageUrl字段更新有问题
        Product product = new Product();

        BeanUtils.copyProperties(productVO, product);

        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public PageVO<Product> findProducts(Integer pageNum, Integer pageSize) {
        //已经拥有的
        PageHelper.startPage(pageNum, pageSize);

        Example o = new Example(Product.class);
        Example.Criteria criteria = o.createCriteria();
        criteria.andLike("status", "%" + 0 + "%");

        List<Product> productList = productMapper.selectByExample(o);
        PageInfo<Product> info = new PageInfo<>(productList);

        return new PageVO<>(info.getTotal(), productList);
    }


}
